﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Windows.Forms.Design;
using System.ComponentModel;
using System.Drawing;

namespace TR0217.ControlEx
{
    [ToolboxBitmap(typeof(NumericUpDown))]
    [ToolStripItemDesignerAvailability(ToolStripItemDesignerAvailability.All)]
    public class ToolStripNumericUpDown : ToolStripControlHost
    {
        public ToolStripNumericUpDown()
            : base(CreateControlInstance())
        {
        }

        public ToolStripNumericUpDown(string name)
            : this()
        {
            base.Name = name;
        }

        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public NumericUpDown NumericUpDown
        {
            get { return base.Control as NumericUpDown; }
        }

        [Bindable(true)]
        [DefaultValue(typeof(decimal),"0")]
        public decimal Value
        {
            get { return NumericUpDown.Value; }
            set { NumericUpDown.Value = value; }
        }

        [DefaultValue(0)]
        public int DecimalPlaces
        {
            get { return NumericUpDown.DecimalPlaces; }
            set { NumericUpDown.DecimalPlaces = value; }
        }

        [DefaultValue(false)]
        public bool Hexadecimal
        {
            get { return NumericUpDown.Hexadecimal; }
            set { NumericUpDown.Hexadecimal = value; }
        }

        [DefaultValue(typeof(decimal), "1")]
        public decimal Increment
        {
            get { return NumericUpDown.Increment; }
            set { NumericUpDown.Increment = value; }
        }

        [DefaultValue(typeof(decimal), "100")]
        public decimal Maximum
        {
            get { return NumericUpDown.Maximum; }
            set { NumericUpDown.Maximum = value; }
        }

        [DefaultValue(typeof(decimal), "0")]
        public decimal Minimum
        {
            get { return NumericUpDown.Minimum; }
            set { NumericUpDown.Minimum = value; }
        }

        [DefaultValue(false)]
        public bool ThousandsSeparator
        {
            get { return NumericUpDown.ThousandsSeparator; }
            set { NumericUpDown.ThousandsSeparator = value; }
        }

        [DefaultValue(false)]
        public bool ReadOnly
        {
            get { return NumericUpDown.ReadOnly; }
            set { NumericUpDown.ReadOnly = value; }
        }

        [Localizable(true)]
        [DefaultValue(typeof(HorizontalAlignment), "0")]
        public HorizontalAlignment NumericUpDownTextAlign
        {
            get { return NumericUpDown.TextAlign; }
            set { NumericUpDown.TextAlign = value; }
        }

        [Localizable(true)]
        [DefaultValue(typeof(LeftRightAlignment), "1")]
        public LeftRightAlignment UpDownAlign
        {
            get { return NumericUpDown.UpDownAlign; }
            set { NumericUpDown.UpDownAlign = value; }
        }

        [DefaultValue(true)]
        public bool InterceptArrowKeys
        {
            get { return NumericUpDown.InterceptArrowKeys; }
            set { NumericUpDown.InterceptArrowKeys = value; }
        }

        [Browsable(false)]
        [Bindable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        public override string Text
        {
            get
            {
                return base.Text;
            }
            set
            {
                base.Text = value;
            }
        }

        protected override Padding DefaultMargin
        {
            get
            {
                if (base.IsOnDropDown)
                {
                    return new Padding(1);
                }
                return new Padding(1, 0, 1, 0);
            }
        }

        protected override Size DefaultSize
        {
            get
            {
                return new Size(100, 0x16);
            }
        }

        public void DownButton()
        {
            NumericUpDown.DownButton();
        }

        public void UpButton()
        {
            NumericUpDown.UpButton();
        }

        protected override void OnSubscribeControlEvents(Control control)
        {
            base.OnSubscribeControlEvents(control);
        }

        protected override void OnUnsubscribeControlEvents(Control control)
        {
            base.OnUnsubscribeControlEvents(control);
        }

        private static Control CreateControlInstance()
        {
            NumericUpDown numericUpDownEx = new NumericUpDown();
            numericUpDownEx.AutoSize = true;
            return numericUpDownEx;
        }
    }
}
